package org.example.sdk;

import java.util.function.Supplier;

class UserStore implements Store<User> {

    private final MutableIndex<Integer, User> primaryKey = new BaseIndex<>() {
        @Override
        public Object getKeyValue(User user) {
            return user.getId();
        }
    };

    private final MutableIndex<MultiKey<Integer, Integer>, User> indexUidOrd = new BaseIndex<>() {
        @Override
        public Object getKeyValue(User user) {
            return new MultiKey<>(user.getUid(), user.getOrd());
        }
    };

    public UserStore(User... users) {
        for (User user : users) {
            add(user);
        }
    }

    public UserStore(Supplier<User[]> supplier) {
        for (User user : supplier.get()) {
            add(user);
        }
    }

    @Override
    public Index<User> getIndex(int i) {
        return switch (i) {
            case 1 -> primaryKey;
            case 2 -> indexUidOrd;
            default -> null;
        };
    }

    @Override
    public void add(User user) {
        primaryKey.put(user.getId(), user);
        indexUidOrd.put(new MultiKey<>(user.getUid(), user.getOrd()), user);
    }
}
